package com.ygou.service.impl;

import java.security.NoSuchAlgorithmException;
import java.sql.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.mail.MessagingException;
import javax.mail.internet.AddressException;

import org.apache.tools.ant.taskdefs.SendEmail;
import org.mybatis.spring.MyBatisExceptionTranslator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ygou.mapper.UsersMapper;
import com.ygou.pojo.Users;
import com.ygou.pojo.UsersExample;
import com.ygou.pojo.UsersExample.Criteria;
import com.ygou.service.UsersService;
import com.ygou.utils.MD5Util;
import com.ygou.utils.MailUtil;
import com.ygou.utils.Tools;

@Service
public class UsersServiceImpl implements UsersService {

	@Autowired
	UsersMapper usersMapper;
	@Override
	public boolean insert(Users record) throws AddressException, NoSuchAlgorithmException, MessagingException {
		record.setLoginpwd(MD5Util.getMd5(record.getLoginpwd()));
		record.setUserphoto("default.png");
        //调用发送邮件服务--函数在步骤2
    	record = MailUtil.activateMail(record);
    	boolean flag=false;
    	try {
    		flag = usersMapper.insert(record)>0;
		} catch (Exception e) {
			try {
				throw new Exception("捕获一个异常");
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}
		return flag;
	}
	@Override
	public List<Users> findByWhere(String userName,String createTime,Integer rankId) {
		UsersExample example=new UsersExample();
		Criteria c=example.createCriteria();
		if(!"".equals(userName)&&userName!=null){
			c.andUsernameLike("%"+userName+"%");
			
		}
		if(createTime!=null &&!"".equals(createTime)){
			c.andCreatetimeLike("%"+createTime+"%");
		}
		if(!"".equals(rankId)&&rankId!=null){
			c.andRankidEqualTo(rankId);
		}
		return usersMapper.findByWhere(example);
	}
	@Override
	public boolean updateByPrimaryKey(Users record) {
		return usersMapper.updateByPrimaryKey(record)>0;
	}
	@Override
	public boolean updateByPrimaryKeySelective(Users record) {
		return usersMapper.updateByPrimaryKeySelective(record)>0;
	}
	@Override
	public Users selectByPrimaryKey(Integer userid) {
		return usersMapper.selectByPrimaryKey(userid);
	}
	@Override
	public Users findUserInfoByWhere(Map<String, Object> map) {
		return usersMapper.findUserInfoByWhere(map);
	}
	@Override
	public int countByExample() {
		return usersMapper.countByExample(null);
	}

}
